package com.source.barcodeMachineStatus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineStatus.entity.BarcodeMachineAlert;
import com.source.barcodeMachineStatus.service.BarcodeMachineAlertService;
import com.source.barcodeMachineStatus.vo.BarcodeMachineAlertVo;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fengq
 * @since 2021-02-27
 */
@Api(tags = "脚标机监控报警信息管理")
@RestController
@RequestMapping("barcodeMachineStatus/barcodeMachineAlert")
@CrossOrigin
public class BarcodeMachineAlertController {
    @Autowired
    private BarcodeMachineAlertService barcodeMachineAlertService;

    @ApiOperation("分页查询，pageCurrent当前页，pageSize每页的记录数 默认值为1/10")
    @GetMapping("/findPage")
    public CommonResult<IPage<BarcodeMachineAlertVo>> findPage(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageCurrent,
                                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<BarcodeMachineAlertVo> page = new Page<>(pageCurrent, pageSize);
        IPage<BarcodeMachineAlertVo> pageVo = barcodeMachineAlertService.getPageVo(page);

        return new CommonResult<IPage<BarcodeMachineAlertVo>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), pageVo);

    }

    @ApiOperation("根据id和进行单个查询")
    @GetMapping("getOneVoById/{alterId}")
    public CommonResult<BarcodeMachineAlertVo> getOneVoById(@PathVariable("alterId") String alterId) {
        BarcodeMachineAlertVo barcodeMachineAlertVo = barcodeMachineAlertService.getOneVoById(alterId);

        return new CommonResult<BarcodeMachineAlertVo>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), barcodeMachineAlertVo);

    }

    @ApiOperation("根据id标记该信息已读")
    @PutMapping("readById")
    public CommonResult<BarcodeMachineAlert> readById(@RequestBody BarcodeMachineAlert barcodeMachineAlert) {
        barcodeMachineAlert.setIsRead(1);
        if (barcodeMachineAlertService.updateById(barcodeMachineAlert)) {
            return new CommonResult<BarcodeMachineAlert>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), barcodeMachineAlert);
        } else {
            return new CommonResult<BarcodeMachineAlert>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), barcodeMachineAlert);
        }
    }

    @ApiOperation(value = "根据ids标记该信息已读", notes = "ids表示批量读取的列表,读取的数量默认最大为1000 ")
    @PutMapping("readBatchByIds")
    public CommonResult<List<BarcodeMachineAlert>> readBatchByIds(@RequestParam("ids") List<BarcodeMachineAlert> ids) {
        if (barcodeMachineAlertService.updateBatchById(ids)) {
            return new CommonResult<List<BarcodeMachineAlert>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ids);
        } else {
            return new CommonResult<List<BarcodeMachineAlert>>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), ids);
        }
    }

    @ApiOperation(value = "根据id删除信息")
    @DeleteMapping("/deleteById/{id}")
    public CommonResult<List<BarcodeMachineAlert>> deleteById(@PathVariable("id") String id) {
        if (barcodeMachineAlertService.removeById(id)) {
            return new CommonResult<List<BarcodeMachineAlert>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
        } else {
            return new CommonResult<List<BarcodeMachineAlert>>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg());
        }
    }

    @ApiOperation(value = "根据ids删除信息")
    @DeleteMapping("/deleteByIds")
    public CommonResult<Boolean> deleteByIds(@RequestParam("list") List<String> list) {
        if (barcodeMachineAlertService.removeByIds(list)) {
            return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
        } else {
            return new CommonResult<Boolean>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg());
        }
    }

    @ApiOperation(value = "多字段模糊查询")
    @GetMapping("/getVoLike")
    public CommonResult<IPage<BarcodeMachineAlertVo>> getVoLike(@RequestParam(value = "pageCurrent", required = false, defaultValue = "1") Integer pageCurrent,
                                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                                @RequestParam(value = "keyword" ,required = false) String keyword) {
        Page<BarcodeMachineAlertVo> page = new Page<>(pageCurrent, pageSize);
        IPage<BarcodeMachineAlertVo> likeVo = barcodeMachineAlertService.getLikeVo(page, keyword);

        return new CommonResult<IPage<BarcodeMachineAlertVo>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), likeVo);

    }

}

